﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    public abstract class AbEvalSettingsLogic : CommonLogic<EvalSettings, int>, IEvalSettingsLogic
    {
        #region Implementation of ICommonLogic<EvalSettings, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(EvalSettings entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(EvalSettings entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int Delete(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("EvalSettings.Delete", ht);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("EvalSettings.Delete", ht);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public EvalSettings FindById(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<EvalSettings>("EvalSettings.FindById", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int FindCount(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByCount("EvalSettings.FindCount", ht);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<EvalSettings> FindAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<EvalSettings>("EvalSettings.Find", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<EvalSettings> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        /// <summary>
        /// 查询评估设置列表
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<EvalSettingsExtend> FindExtend(string schoolMark)
        {
            return FindExtendGrade(EvalType.Teacher, schoolMark);
        }

        /// <summary>
        /// 查询评估设置列表
        /// </summary>
        /// <param name="evalType"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<EvalSettingsExtend> FindExtendGrade(EvalType evalType, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("EvalType", evalType);
            return FindCustomer<EvalSettingsExtend>("EvalSettings.FindExtend", ht);
        }

        /// <summary>
        /// 查询评估设置列表
        /// </summary>
        /// <param name="causeId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<EvalSettingsExtend> FindExtend(int causeId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            if (causeId > 0)
                ht.Add("causeID", causeId);
            return FindCustomer<EvalSettingsExtend>("EvalSettings.FindExtendByCauseId", ht);
        }

        /// <summary>
        /// 查询评估设置列表
        /// </summary>
        /// <param name="gradeIndex"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<EvalSettingsExtend> FindExtendGrade(int gradeIndex, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            if (gradeIndex > 0)
                ht.Add("GradeIndex", gradeIndex);
            return FindCustomer<EvalSettingsExtend>("EvalSettings.FindExtendByGradeIndex", ht);
        }

        /// <summary>
        /// 查询评估设置
        /// </summary>
        /// <param name="id"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public EvalSettingsExtend FindExtendById(int id, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", id);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<EvalSettingsExtend>("EvalSettings.FindExtendById", ht);
        }

        /// <summary>
        /// 获取本校教研组
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<GlobalCauseInfo> GetResearchGroupList(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<GlobalCauseInfo>("EvalSettings.GetResearchGroupList", ht);
        }

        /// <summary>
        /// 获取本校年级组
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<int> GetGradeList(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<int>("EvalSettings.GetGradeList", ht);
        }

        /// <summary>
        /// 插入评估设置
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="teachCode"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int SaveEvalSettings(EvalSettings settings, string teachCode, string schoolMark)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("EvalSettings", settings);
                ht.Add("TeachCode", teachCode);
                ht.Add("SchoolMark", schoolMark);

                int id = (int)InsertCustomer("EvalSettings.SaveEvalSettings", ht);
                return id;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        /// <summary>
        /// 更新评估设置
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="teachCode"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int UpdateEvalSettings(EvalSettings settings, string teachCode, string schoolMark)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("EvalSettings", settings);
                ht.Add("TeachCode", teachCode);
                ht.Add("SchoolMark", schoolMark);

                return UpdateCustomer("EvalSettings.UpdateEvalSettings", ht);
            }
            catch (Exception)
            {
                return -1;
            }
        }

        /// <summary>
        /// 判断用户是否已被设置
        /// </summary>
        /// <param name="id"></param>
        /// <param name="teachCode"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public bool ExistsTeacher(int id, string teachCode, string schoolMark)
        {
            return ExistsTeacher(id, teachCode, schoolMark, EvalType.Teacher);
        }

        /// <summary>
        /// 判断用户是否已被设置
        /// </summary>
        /// <param name="id"></param>
        /// <param name="teachCode"></param>
        /// <param name="schoolMark"></param>
        /// <param name="evalType"></param>
        /// <returns></returns>
        public bool ExistsTeacher(int id, string teachCode, string schoolMark, EvalType evalType)
        {
            try
            {
                Hashtable ht = new Hashtable();
                if (id > 0)
                {
                    ht.Add("RID", id);
                }
                ht.Add("TeachCode", teachCode);
                ht.Add("SchoolMark", schoolMark);
                ht.Add("EvalType", evalType);

                return FindCustomerByID<int>("EvalSettings.ExistsTeacher", ht) > 0;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断用户是否已被设置
        /// </summary>
        /// <param name="teachID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public bool ExistsTeacher(int teachID, string schoolMark)
        {
            return ExistsTeacher(teachID, schoolMark, EvalType.Teacher);
        }

        /// <summary>
        /// 判断用户是否已被设置
        /// </summary>
        /// <param name="teachID"></param>
        /// <param name="schoolMark"></param>
        /// <param name="evalType"></param>
        /// <returns></returns>
        public bool ExistsTeacher(int teachID, string schoolMark, EvalType evalType)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TeacherID", teachID);
                ht.Add("SchoolMark", schoolMark);
                ht.Add("EvalType", evalType);

                return FindCustomerByID<int>("EvalSettings.ExistsTeacherID", ht) > 0;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 获取教研组列表字符串
        /// </summary>
        /// <param name="teacherID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public string GetResearchIDStringByTeacher(int teacherID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TeachID", teacherID);
            ht.Add("SchoolMark", schoolMark);
            string result = FindCustomerByID<string>("EvalSettings.GetResearchIDListByTeacher", ht);
            return result == null ? null : result.TrimEnd(',');
        }

        /// <summary>
        /// 获取教研组列表
        /// </summary>
        /// <param name="teacherID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<int> GetResearchIDListByTeacher(int teacherID, string schoolMark)
        {
            string str = GetResearchIDStringByTeacher(teacherID, schoolMark);
            string[] idArr = str.Split(',');
            IList<int> list = new List<int>();
            foreach (string arr in idArr)
            {
                int id;
                bool flag = int.TryParse(arr, out id);
                if (!flag) continue;
                list.Add(id);
            }
            return list;
        }

        /// <summary>
        /// 获取年级组列表字符串
        /// </summary>
        /// <param name="teacherID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public string GetGradeIndexStringByTeacher(int teacherID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TeachID", teacherID);
            ht.Add("SchoolMark", schoolMark);
            string result = FindCustomerByID<string>("EvalSettings.GetGradeIndexListByTeacher", ht);
            return result == null ? null : result.TrimEnd(',');
        }

        /// <summary>
        /// 获取年级组列表
        /// </summary>
        /// <param name="teacherID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<int> GetGradeIndexListByTeacher(int teacherID, string schoolMark)
        {
            string str = GetGradeIndexStringByTeacher(teacherID, schoolMark);
            string[] idArr = str.Split(',');
            IList<int> list = new List<int>();
            foreach (string arr in idArr)
            {
                int id;
                bool flag = int.TryParse(arr, out id);
                if (!flag) continue;
                list.Add(id);
            }
            return list;
        }

        /// <summary>
        /// 查询评价分配明细
        /// </summary>
        /// <param name="causeID"></param>
        /// <param name="evalType"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public abstract DataTable GetSettingsDetail(int causeID, EvalType evalType, string schoolMark);

        /// <summary>
        /// 查询评价分配明细
        /// </summary>
        /// <param name="gradeIndex"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public abstract DataTable GetSettingsDetailGrade(int gradeIndex, string schoolMark);
    }
}
